package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.rmi.runtime.Log;

import java.time.LocalDateTime;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;


    //添加购物车
    @PostMapping("/add")
    public R<String> add(@RequestBody ShoppingCart shoppingCart){
        //查询当前菜品或者套餐是否在购物车中
        QueryWrapper<ShoppingCart> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq(shoppingCart.getDishId()!=null,"dish_id",shoppingCart.getDishId());
        queryWrapper.eq(shoppingCart.getSetmealId()!=null,"setmeal_id",shoppingCart.getSetmealId());
        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);
        //判断是否存在
        if (cart != null) {
            //如果已经存在，就在原来数量基础上加一
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartService.updateById(cart);
        } else {
            //如果不存在，则添加到购物车，数量默认就是一
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setUserId(BaseContext.getCurrentId());//设置用户id
            shoppingCartService.save(shoppingCart);
        }
        return  R.success("添加购物车成功");
    }

    //查询购物车
    @GetMapping("list")
    public R<List<ShoppingCart>> list() {
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", BaseContext.getCurrentId());
        queryWrapper.orderByAsc("create_time");
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }

    //清空购物车
    @DeleteMapping("clean")
    public R<String> clean() {
        //SQL:delete from shopping_cart where user_id = ?
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", BaseContext.getCurrentId());
        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车成功");
    }

    //减一份
    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
       QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(shoppingCart.getDishId()!=null,"dish_id",shoppingCart.getDishId());
        queryWrapper.eq(shoppingCart.getSetmealId()!=null,"setmeal_id",shoppingCart.getSetmealId());
        queryWrapper.eq("user_id", BaseContext.getCurrentId());
       ShoppingCart cart = shoppingCartService.getOne(queryWrapper);
       if(cart.getNumber()>1){
           cart.setNumber(cart.getNumber()-1);
           shoppingCartService.updateById(cart);
       }else {
           shoppingCartService.removeById(cart);
       }
       return R.success("减一份");
    }

}
